import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import envFuncs from '@/config/base'
let env = envFuncs.getParams()
import { UPDATE_MSG_COUNT } from '../mutation-types'

export default {
  getChildSource ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U()
    let idParam = ''
    if (typeof info.id !== 'undefined') {
      idParam = '/' + info.id
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.app.token + '/sources/child' + idParam + frag
      ).then((response) => {
        success(response.data)
      }).catch((err) => {
        success({
          errcode: 10000,
          results: null,
          message: err.getMessage()
        })
        error(err)
        console.log(err)
      })
    })
  },
  getManagableChildSource ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U()
    let idParam = ''
    if (typeof info.id !== 'undefined') {
      idParam = '/' + info.id
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.app.token + '/sources/managable_child' + idParam + frag
      ).then((response) => {
        success(response.data)
      }).catch((err) => {
        success({
          errcode: 10000,
          results: null,
          message: err.getMessage()
        })
        error(err)
        console.log(err)
      })
    })
  }
}
